﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Koeltp.DataAccess;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;

/// <summary>
///DataBind 的摘要说明
/// </summary>
public class DataBind
{
    IDataAccess da;
    public DataBind()
    {
        da = IDataAccessFactory.CreateDataAccess();
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="articleType">文章类型，如1为code类、2为database类等，""为查询所有</param>
    /// <param name="pageIndex">当前页的索引</param>
    /// <param name="pageSize">每页显示多少条数据</param>
    /// <returns></returns>
    public DataSet ArticleListGetBind(string articleType, Int32 pageIndex, Int32 pageSize)
    {
        String sql = "P_ArticleListPaging";
        QueryParameter[] paras = new QueryParameter[4];
        paras[0] = new QueryParameter("@articleType", articleType, DbType.String);

        paras[1] = new QueryParameter("@pageIndex", pageIndex, DbType.Int32);
        paras[2] = new QueryParameter("@pageSize", pageSize, DbType.Int32);
        paras[3] = new QueryParameter("@Article_Status", true, DbType.Boolean);
        DataSet ds = da.ArticleListPaging(sql, paras);
        return ds;
    }
    /// <summary>
    /// 热门文章
    /// </summary>
    /// <param name="rpt"></param>
    public void TopArticleGetBind(Repeater rpt)
    {
        String sql = "select top(select ShowNumber_Count from dbo.T_ShowNumber where ShowNumber_Id=1) article_id,article_title,comment_count from dbo.V_ArtcleList order by article_readerNumber desc ,comment_count desc";
        ControlBind(sql, rpt, CommandType.Text);
    }
    /// <summary>
    /// 友情链接
    /// </summary>
    /// <param name="rpt"></param>
    public void LinksGetBind(Repeater rpt)
    {
        String sql = "select top(select ShowNumber_Count from dbo.T_ShowNumber where ShowNumber_Id=2) Links_Name,Links_Url from dbo.T_Links order by Links_Sort asc";
        ControlBind(sql, rpt, CommandType.Text);
    }
    /// <summary>
    /// 系列文章
    /// </summary>
    /// <param name="rpt"></param>
    public void SeriesArticleGetBind(Repeater rpt)
    {
        //select Article_Id,SeriesArticle_Name ,dbo.F_CountComment(Article_Id) Comment_Count from dbo.T_SeriesArticle
        string sql = "select top(select ShowNumber_Count from dbo.T_ShowNumber where ShowNumber_Id=3) Article_Id,SeriesArticle_Name ,dbo.F_CountComment(Article_Id) Comment_Count from dbo.T_SeriesArticle";
        ControlBind(sql, rpt, CommandType.Text);
    }
    /// <summary>
    /// 导航
    /// </summary>
    /// <param name="rpt"></param>
    /// <param name="articleId"></param>
    public void NavGetBind(Repeater rpt, int articleId)
    {
        String sql = "SELECT articleType_Name,articleType_nickName,dbo.F_CountComment(Article_Id) as 'Comment_Count',article_title,article_id FROM dbo.V_ArtcleList where article_id=" + articleId;
        ControlBind(sql, rpt, CommandType.Text);
    }
    
    /// <summary>
    /// 到要要详细阅读的一篇文章
    /// </summary>
    /// <param name="rpt"></param>
    /// <param name="articleId"></param>
    public void GetBindArticle(Repeater rpt, int articleId)
    {
        String sql = "select article_title, article_id, users_id, Article_StartTime,articleType_id,ArticleType_NickName,Users_Introduction,Users_Email,Users_AvatarUrl,articleType_Name,article_author, article_readerNumber ,comment_count,article_content FROM dbo.V_ArtcleList where article_id=" + articleId;
        ControlBind(sql, rpt, CommandType.Text);
    }
    /// <summary>
    /// 绑定评论控件
    /// </summary>
    /// <param name="rpt"></param>
    /// <param name="articleId"></param>
    public void CommentGetBind(Repeater rpt, int articleId)
    {
        String sql = "select comment_id,article_id,Comment_DateTime,comment_name,comment_content,comment_url,(case when dbo.F_AvatarUrl(comment_id)<>'NULL' then dbo.F_AvatarUrl(comment_id) ELSE 'defaultAvatar.png' end) as AvatarUrl from dbo.T_Comment where article_id=" + articleId;
        ControlBind(sql, rpt, CommandType.Text);
    }
    /// <summary>
    /// 评论总数
    /// </summary>
    /// <returns></returns>
    public Int32 CommentCount()
    {
        String sql = "select count(*) from T_Comment";
        object obj = da.GetScalar(sql, CommandType.Text);
        return Convert.ToInt32(obj);

    }
    public Int32 ArticleCount()
    {
        String sql = "select count(*) from T_Article";
        object obj = da.GetScalar(sql, CommandType.Text);
        return Convert.ToInt32(obj);
    }
    /// <summary>
    /// 控件绑定
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="rpt"></param>
    private void ControlBind(String sql, Repeater rpt, CommandType commandType)
    {
        DataTable dt = new DataTable();
        dt = da.GetTable(sql, dt, commandType);
        rpt.DataSource = dt;
        rpt.DataBind();
        dt.Dispose();
    }
    /// <summary>
    /// 插入评论
    /// </summary>
    /// <param name="Comment_Name">评论者昵称</param>
    /// <param name="Comment_eMail">评论者的E-Mail</param>
    /// <param name="Comment_Url">评论者的网站</param>
    /// <param name="Comment_Content">评论内容</param>
    /// <param name="Users_Id">如果是注册用户就传个ID号，默认传个0表示不是注册用户。</param>
    /// <returns></returns>
    public Int32 InsertComment(int articleId,String Comment_Name, String Comment_eMail, String Comment_Url, String Comment_Content, Int32 Users_Id)
    {
        QueryParameter[] paras = new QueryParameter[6];

        String sqlA = "insert into T_Comment(Article_Id,Comment_Name,Comment_Content";
        String sqlB = "values(@Article_Id,@Comment_Name,@Comment_Content";
        paras[0]=new QueryParameter("@Article_Id",articleId,DbType.Int32);
        paras[1]=new QueryParameter("@Comment_Name",Comment_Name,DbType.String);
        paras[2]=new QueryParameter("@Comment_Content",Comment_Content,DbType.String);
        if (!String.IsNullOrEmpty(Comment_eMail))
        {
            sqlA += ",Comment_eMail";
            sqlB += ",@Comment_eMail";
            paras[3]=new QueryParameter("@Comment_eMail",Comment_eMail,DbType.String);
        }
        if (!String.IsNullOrEmpty(Comment_Url))
        {
            sqlA += ",Comment_Url";
            sqlB += ",@Comment_Url";
            paras[4] = new QueryParameter("@Comment_Url", Comment_Url, DbType.String);
        }
        if (Users_Id != 0)
        {
            sqlA += ",Users_Id";
            sqlB += ",@Users_Id";
            paras[5] = new QueryParameter("@Users_Id", Users_Id, DbType.String);
        }
        sqlA += ") ";
        sqlB += ") ";
        String sql = sqlA + sqlB;
        int status =da.ExecuteNonQuery(sql, CommandType.Text, paras);
        return status;
    }

    public void UpdateReaderNumber(Int32 articleId)
    {
        String sql = "UPDATE dbo.T_Article set Article_ReaderNumber=Article_ReaderNumber+1 ";
        QueryParameter[] paras = new QueryParameter[1];
        if (!String.IsNullOrEmpty(sql))
        {
            sql += "where Article_Id=@Article_Id ";
            paras[0] = new QueryParameter("@Article_Id", articleId, DbType.Int32);
        }
        int status;
        try
        {
           status= da.ExecuteNonQuery(sql, CommandType.Text, paras);
        }
        catch (Exception ex)
        {
            
        }
    }
}